/*********************************************************************
  Pick one up today in the adafruit shop!
  ------> http://www.adafruit.com/category/63_98
*********************************************************************/
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);

#if (SSD1306_LCDHEIGHT != 64)
#error("Height incorrect, please fix Adafruit_SSD1306.h!");
#endif

void setup()   {                
  Serial.begin(115200);
  Serial.println("Setup begin");
  
  getCurrent(); //Init ACS Current sensor
  
  // by default, we'll generate the high voltage from the 3.3v line internally! (neat!)
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);  // initialize with the I2C addr 0x3D (for the 128x64)
  // init done
  
  // Clear the buffer.
  display.clearDisplay();
  display.setTextSize(2);
  display.setTextColor(WHITE);
  display.display();
  pinMode(9,OUTPUT);
  Serial.println("Setup end");
}

unsigned long tLoop;
//Termometer bitmap
static const unsigned char PROGMEM tempIcon[] = { B00000000, B11000000,
  B00000001, B11000000,
  B00000001, B11000000,
  B00000011, B11100000,
  B11110011, B11100000,
  B11111110, B11111000,
  B01111110, B11111111,
  B00110011, B10011111,
  B00011111, B11111100,
  B00001101, B01110000,
  B00011011, B10100000,
  B00111111, B11100000,
  B00111111, B11110000,
  B01111100, B11110000,
  B01110000, B01110000,
  B00000000, B00110000 };

void loop() {
  unsigned long tLoop1 = constrain(millis()-tLoop-500,0,999);
  Serial.println(tLoop1);
  
  int power = analogRead(A1);
  power=map(power, 0, 255, 0, 1024); 
  analogWrite(9,power);
  
  display.clearDisplay();
  display.setCursor(0,0);
  display.print(tLoop1);display.setTextSize(1);display.print("looptime");display.setTextSize(2);display.println("");
  display.drawBitmap(128-16, 1, tempIcon, 16, 16, 1);
  display.print((int)getTemp());display.setTextSize(1);display.print("temp");display.setTextSize(2);display.println("");
  display.print(getCurrent()); display.print(" (");display.print(analogRead(A0));display.print(")");
  display.print(power);display.setTextSize(1);display.print("power");display.setTextSize(2);
  display.display();
  digitalWrite(13, !digitalRead(13));
  tLoop = millis();
  delay(500);  
}


